package com.starmark.sys.org.atomic.web;


import com.starmark.core.framework.base.AbstractController;
import com.starmark.core.framework.response.Response;
import com.starmark.core.framework.response.SuccessResponse;
import com.starmark.core.framework.util.BeanMapper;
import com.starmark.sys.org.atomic.entity.SysOrgUser;
import com.starmark.sys.org.atomic.entity.SysOrgUserDeptRel;
import com.starmark.sys.org.atomic.service.ISysOrgDeptService;
import com.starmark.sys.org.atomic.service.ISysOrgUserDeptRelService;
import com.starmark.sys.org.atomic.service.ISysOrgUserService;
import com.starmark.sys.org.atomic.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * 用户管理控制器
 */
@RestController
@Api(value = "用户管理", description = "用户管理")
@RequestMapping(value = "/sys/org/user")
public class SysOrgUserController extends AbstractController {

    @Autowired
    private ISysOrgUserService sysOrgUserService;

    @Autowired
    private ISysOrgDeptService sysOrgDeptService;

    @Autowired
    private ISysOrgUserDeptRelService sysOrgUserDeptRelService;

    @ApiOperation(value = "查询用户")
    @PostMapping(value = "/query")
    public Object query(@RequestBody SysOrgUserPageQueryVo sysOrgUserPageQueryVo) {

        return BeanMapper.mapPage(sysOrgUserService.selectUserPage(sysOrgUserPageQueryVo), SysOrgUserPageVo.class);
    }


    @ApiOperation(value = "新增用户")
    @PostMapping
    public SysOrgUserVo save(@RequestBody SysOrgUserVo sysOrgUserVo) {
        String userId = sysOrgUserService.saveUserInfo(sysOrgUserVo);
        sysOrgUserVo.setId(userId);
        return sysOrgUserVo;
    }

    @ApiOperation(value = "用户详情")
    @GetMapping(value = "/{id}")
    public SysOrgUserVo get(@PathVariable("id") String id) {
        SysOrgUser sysOrgUser = sysOrgUserService.getById(id);
        sysOrgUser.setPassword(null);
        SysOrgUserVo sysOrgUserVo = BeanMapper.map(sysOrgUser, SysOrgUserVo.class);
        List<SysOrgUserDeptRel> userDeptRels = sysOrgUserDeptRelService.findUserDeptRels(id);
        if (!CollectionUtils.isEmpty(userDeptRels)) {
            List<SysOrgUserDeptRelVo> sysOrgUserDeptRelVos = BeanMapper.mapList(userDeptRels, SysOrgUserDeptRelVo.class);
            List<String> deptIds = sysOrgUserDeptRelVos.stream().map(SysOrgUserDeptRelVo::getDeptId).collect(Collectors.toList());
            Map<String, String> deptMap = sysOrgDeptService.findDeptMapByIds(deptIds);
            for (SysOrgUserDeptRelVo sysOrgUserDeptRelVo : sysOrgUserDeptRelVos) {
                sysOrgUserDeptRelVo.setDeptName(deptMap.get(sysOrgUserDeptRelVo.getDeptId()));
            }
            sysOrgUserVo.setUserDeptRel(sysOrgUserDeptRelVos);
        }
        return sysOrgUserVo;

    }

    @PutMapping
    @ApiOperation(value = "修改用户")
    public Object update(@RequestBody SysOrgUserVo sysOrgUserVo) {
        sysOrgUserService.updateUserInfo(sysOrgUserVo);
        return sysOrgUserVo;
    }

    @PostMapping("updatePwd")
    @ApiOperation(value = "修改用户密码")
    public Response updatePwd(@RequestBody SysOrgUserChangePwdVo sysOrgUserChangePwdVo) {
        sysOrgUserService.updatePwd(sysOrgUserChangePwdVo.getId(), sysOrgUserChangePwdVo.getPassword());
        return SuccessResponse.newInstance();
    }


    @DeleteMapping(value = "/{id}")
    @ApiOperation(value = "删除用户")
    public void delete(@PathVariable("id") String id) {
        sysOrgUserService.removeById(id);
    }


    @PostMapping(value = "/delete")
    @ApiOperation(value = "批量删除用户")
    public void deleteByIds(@RequestBody List<String> ids) {
        sysOrgUserService.removeByIds(ids);


    }
}
